let path = require("path")
const CompressionWebpackPlugin = require("compression-webpack-plugin")
const productionGzipExtensions = ["js", "css"]
const isProduction = process.env.NODE_ENV === "production"
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  //基本路径
  publicPath: "./",
  // 输出文件目录
  outputDir: "../dist",
  // eslint-loader是否在保存的时候检查
  lintOnSave: true,

  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  // pages: {
  //   home: {
  //     // entry for the pages
  //     entry: 'pages/home/index.js',
  //     // the source template
  //     template: 'public/home.html',
  //     // output as dist/index.html
  //     filename: 'home.html',
  //     // when using title option,
  //     // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
  //     title: process.env.VUE_APP_HOME_TITLE,
  //     // chunks to include on this pages, by default includes
  //     // extracted common chunks and vendor chunks.
  //     chunks: ['chunk-vendors', 'chunk-common', 'home']
  //   },
  //   search: {
  //     // entry for the pages
  //     entry: 'pages/search/index.js',
  //     // the source template
  //     template: 'public/search.html',
  //     // output as dist/index.html
  //     filename: 'search.html',
  //     // when using title option,
  //     // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
  //     title: process.env.VUE_APP_SEARCH_TITLE,
  //     // chunks to include on this pages, by default includes
  //     // extracted common chunks and vendor chunks.
  //     chunks: ['chunk-vendors', 'chunk-common', 'search']
  //   }
  // },
  css: {
    // 开启 CSS source maps?
    sourceMap: true,
    // css预设器配置项
    loaderOptions: {
      less: {
        // 若 less-loader 版本小于 6.0，请移除 lessOptions 这一级，直接配置选项。
        // lessOptions: {
          modifyVars: {
            // 直接覆盖变量
            // 'text-color': '#111',
            // 'border-color': 'red',
            // 或者可以通过 less 文件覆盖（文件路径为绝对路径）
          hack: `true; @import "/src/styles/vant.less";`
          },
        // },
      },
    },
  },

  configureWebpack: (config) => {
    Object.assign(config, {
      // 开发生产共同配置
      resolve: {
        extensions: [".js", ".vue", ".json"],
        alias: {
          "@": resolve("src"),
          "@router": resolve("src/router"),
          "@components": resolve("src/components"),
          "@assets": resolve("src/assets"),
          "@store": resolve("src/store"),
          "@utils": resolve("src/utils"),
          "@styles": resolve("src/styles"),
          "@views": resolve("src/views"),
          "@mock": resolve("mock"),
          "@apis": resolve("src/apis"),

          "@home": resolve("pages/home"),
          "@search": resolve("pages/search"),
        }
      }
    })

    if (isProduction) {
      // chunk-vendors不打包该文件，以cdn链接引用
      config.externals = {
        // vue: 'Vue',
        // 'vue-router': 'VueRouter',
        // vuex: 'Vuex',
        // axios: 'axios',
        // echarts: 'echarts'
      }

      //开启Gzip——还需测试
      config.plugins.push(
        new CompressionWebpackPlugin({
          algorithm: "gzip",
          test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
          threshold: 10240, //超过10K进行压缩
          minRatio: 0.8,
          deleteOriginalAssets: false //是否删除源文件
        })
      )
    } else { 
      config.devtool="eval-cheap-source-map"
    }
  },
  chainWebpack(config) { 
    config.plugin('html')
        .tap(args => {
          args[0].title = process.env.VUE_APP_TITLE;
          return args;
        })
  },

  devServer: {
    before: require('./mock/mock-server.js'),
    host: "0.0.0.0",
    port: 8881, // 端口
    // open: true,
    // proxy: 'http://172.16.70.110:8080' // 设置代理
    // proxy: {
    //   '/zcwx': {
    //     target: 'http://172.16.6.99:8082',
    //     // target: 'http://zc.tpri.org.cn',
    //     changeOrigin: true,
    //     // ws: true,
    //     pathRewrite: {
    //       '^/zcwx': '/zcwx'
    //     }
    //   }
    // }
  },

  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [resolve("src/styles/variable.less")]
    }
  }
}
